package com.sky.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品接口")
public class DishController {
    @Resource
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "setmealCache", key = "#dish.categoryId")
    public Result<String> add(@RequestBody Dish dish) {
        log.info("菜品数据：{}", dish);

        dishService.add(dish);

        return Result.success("新增成功");
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> del(@RequestParam List<Integer> ids) {
        try {
            dishService.del(ids);

            return Result.success("删除菜品成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> edit(@RequestBody Dish dish) {
        log.info("菜品数据：{}", dish);

        dishService.edit(dish);
        return Result.success("修改菜品成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("查询菜品详情")
    public Result<Dish> get(@PathVariable Integer id) {
        return Result.success(dishService.getById(id));
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> CateByIdDish(Integer categoryId) {
        List<Dish> data = dishService.CateByIdDish(categoryId);

        return Result.success(data);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<Page<Dish>> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页数据：{}", dishPageQueryDTO);

        return Result.success(dishService.page(dishPageQueryDTO));
    }
}
